from appium.options.common import AppiumOptions
from appium import webdriver
from appium.options.common.base import AppiumOptions
from appium.webdriver.common.appiumby import AppiumBy


class BaseDriver:
    """
    基础驱动类，采用单例模式设计，确保全局只有一个驱动实例
    用于创建和管理 Appium 驱动，提供获取驱动实例的方法
    """
    def __new__(cls, *args, **kwargs):
        if not hasattr(cls, 'instance'):
            cls.instance = super().__new__(cls)
        return cls.instance
    def __init__(self):
        if not hasattr(self, 'driver'):
            self.driver = None
    def create_driver(self):
        options = AppiumOptions()
        options.load_capabilities({
            "platformName": "Android",
            "appium:automationName": "UiAutomator2",
            "appium:ensureWebviewsHavePages": True,
            "appium:nativeWebScreenshot": True,
            "appium:newCommandTimeout": 3600,
            "appium:noReset": True,
            "appium:connectHardwareKeyboard": True,
            "appium:appPackage": "com.xiaomi.shop",
            "appium:appActivity": ".activity.MainTabActivity "
        })
        self.driver = webdriver.Remote("http://127.0.0.1:4723", options=options)
        self.driver.implicitly_wait(10)

    def get_driver(self):
        return self.driver

# 创建 BaseDriver 单例实例
base = BaseDriver()
# 调用方法创建驱动
base.create_driver()
# 获取驱动实例并指定类型为 webdriver.Remote
DRIVER: webdriver.Remote = base.get_driver()